import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
//封装游客身份模块uuid--->生成一个随机字符串（不能在变了）
import {getUUID} from '@/utils/uuid_token';
export default {
	namespaced:true,
	actions:{
		//获取商品详情数据
		async getGoodsInfo(context,skuid){
			let result = await reqGoodsInfo(skuid);
			if(result.code == 200){
				context.commit('GETGOODSINFO',result.data);
			}
		},
        //加入购物车或者修改商品数量
		async addOrUpdateShopCart(context,{ skuId, skuNum }){  
		   console.log("skuid=====>",skuId);
		   console.log("skuNum====>",skuNum);
           let result = await reqAddOrUpdateShopCart(skuId,skuNum);
		   if (result.code == 200) {
			//返回的是成功的标记
			return "ok";
		  } else {
			//返回的是失败的标记
			return Promise.reject(new Error("faile"));
		  }      
		}    
	},

	mutations:{
		GETGOODSINFO(state,goodsInfo){
            state.goodsInfo = goodsInfo;
		}   
	},

	state:{
	   //商品信息	
	   goodsInfo:{},
	   //游客临时身份
       uuid_token:getUUID()	
	},

	getters:{
		categoryView(state) {
			//比如:state.goodInfo初始状态空对象，空对象的categoryView属性值undefined
			//当前计算出的 categoryView属性值至少是一个空对象，假的报错不会有了。
			return state.goodsInfo.categoryView || {};
		},
		skuInfo(state) {
			return state.goodsInfo.skuInfo || {};
		},
		spuSaleAttrList(state) {
			return state.goodsInfo.spuSaleAttrList || [];
		},	
	},

}